From 2c6ab4da10070dc6ab304798fbd4d6b24262c42d Mon Sep 17 00:00:00 2001 From: justbur Date: Fri, 17 Jul 2015 10:49:11 -0400 Subject: [PATCH] Add option to sort output by key (off by default) --- which-key.el | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/which-key.el b/which-key.el index b54aedb883f..4cc1272aba7 100644 --- a/which-key.el +++ b/which-key.el @@ -177,6 +177,11 @@ a percentage out of the frame's height." :type '(radio (const :tag "Yes" t) (const :tag "No" nil))) +(defcustom which-key-sort nil + "Sort output by `key-description' if non-nil." + :group 'which-key + :type 'boolean) + ;; Faces (defface which-key-key-face '((t . (:inherit font-lock-constant-face))) @@ -726,6 +731,16 @@ alists. Returns a list (key separator description)." (list key-w-face sep-w-face desc-w-face))) unformatted))) +(defun which-key--key-description< (a b) + "Order key descriptions A and B." + (let ((la (string-width a)) + (lb (string-width b))) + (cond ((and (= la 1) (= lb 1)) (string-lessp a b)) + ((or (= la 1) (= lb 1)) (= la 1)) + ((string-equal (substring a 0 2) (substring b 0 2)) + (which-key--key-description< (substring a 2) (substring b 2))) + (t (string-lessp a b))))) + (defun which-key--get-formatted-key-bindings (buffer key-seq) "Uses `describe-buffer-bindings' to collect the key bindings in BUFFER that follow the key sequence KEY-SEQ." @@ -743,6 +758,10 @@ BUFFER that follow the key sequence KEY-SEQ." desc-match (match-string 2)) (cl-pushnew (cons key-match desc-match) unformatted :test (lambda (x y) (string-equal (car x) (car y)))))) + (when which-key-sort + (setq unformatted + (sort unformatted + (lambda (a b) (which-key--key-description< (car a) (car b)))))) (which-key--format-and-replace unformatted (key-description key-seq)))) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; -- 2.30.2